package com.baymax.test.scene.demo.maverick

import android.net.ConnectivityManager
import android.net.LinkProperties
import android.net.Network
import android.net.NetworkInfo
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.lifecycle.Lifecycle
import com.airbnb.mvrx.MavericksState
import com.airbnb.mvrx.MavericksViewModel
import com.airbnb.mvrx.MavericksViewModelFactory
import com.airbnb.mvrx.ViewModelContext
import com.baymax.test.scene.BaseScene
import com.baymax.test.scene.addButton
import com.baymax.test.scene.addDFView
import com.baymax.test.scene.newSceneFlexContainer
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject


class RequestNetWork : BaseScene() {

    private val viewModel: RequestNetWorkViewModel by sceneViewModel()
    override fun onCreateView(
        p0: LayoutInflater, p1: ViewGroup, p2: Bundle?
    ): View = p1.newSceneFlexContainer {
        addButton("request") {
            viewModel.request()
        }

        val logger = addDFView(-1, -1, TextView(context))

        viewModel.onEach { st ->
            logger.append("$st \n")
        }

    }


    class RequestNetWorkViewModel(val state: State) :
        MavericksViewModel<RequestNetWorkViewModel.State>(state), KoinComponent {

        val connectivityManager: ConnectivityManager by inject()


        data class State(
            val id: Int, val network: Network? = null,
            val networkInfo: NetworkInfo? = null,
            val linkProp: LinkProperties? = null
        ) : MavericksState

        fun request() {
            val current: Network? = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                connectivityManager.activeNetwork
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                connectivityManager.allNetworks.firstOrNull { connectivityManager.getNetworkInfo(it)?.isConnected == true }
            } else null
            val prop = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                connectivityManager.getLinkProperties(current)
            } else {
                null
            }
            val inf0 = connectivityManager.activeNetworkInfo
            println(connectivityManager.allNetworkInfo.joinToString(","))
            setState {
                this.copy(
                    id = id + 1,
                    network = current,
                    networkInfo = inf0,
                    linkProp = prop
                )
            }
        }


        companion object : MavericksViewModelFactory<RequestNetWorkViewModel, State> {
            override fun create(
                viewModelContext: ViewModelContext, state: State
            ): RequestNetWorkViewModel {
                return RequestNetWorkViewModel(state)
            }

            override fun initialState(viewModelContext: ViewModelContext): State? {
                return RequestNetWorkViewModel.State(0)
            }
        }
    }


}